﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Home
{
    public static class Create
    {
        public static Adams.Admin.DataTransfer.Announcements.Create.PageLoad PageLoad(int id)
        {
            var data = new Adams.Admin.DataTransfer.Announcements.Create.PageLoad();
            data.Announcement = new Shared.Entities.Announcements.Announcement();

            if (id > 0)
            {
                using (SqlDataReader dr = DB.RunDataReader(@"
SELECT * FROM Announcements.Announcements WHERE AnnouncementID = @AnnouncementID
", CommandType.Text,
                DB.CreateParameter("@AnnouncementID", id, SqlDbType.Int)))
                {
                    dr.Read();
                    data.Announcement.ID = dr["AnnouncementID"].DbInt();
                    data.Announcement.Title = dr["Title"].DbString();
                    data.Announcement.ShortDescription = dr["ShortDescription"].DbString();
                    data.Announcement.LongDescription = dr["LongDescription"].DbString();
                    data.Announcement.Link = dr["Link"].DbString();
                    data.Announcement.EndDate = dr["EndDate"].DbDate();
                    data.Announcement.StartDate = dr["StartDate"].DbDate();
                }
            }

            return data;
        }

        public static void Save(DataTransfer.Announcements.Create.FormInput input)
        {
            DB.Run(@"
IF(@AnnouncementID > 0)
BEGIN
    UPDATE Announcements.Announcements SET
        Title = @Title
        ,ShortDescription = @ShortDescription
        ,LongDescription = @LongDescription
        ,StartDate = @StartDate
        ,EndDate = @EndDate
        ,Link = @Link
    WHERE AnnouncementID = @AnnouncementID
END
ELSE
BEGIN
    INSERT INTO Announcements.Announcements (
        Title
        ,ShortDescription
        ,LongDescription
        ,StartDate
        ,EndDate
        ,Link)
    VALUES (
        @Title
        ,@ShortDescription
        ,@LongDescription
        ,@StartDate
        ,@EndDate
        ,@Link)
END
    ", CommandType.Text,
                    DB.CreateParameter("@AnnouncementID", input.ID.ToInt(), SqlDbType.Int),
                    DB.CreateParameter("@Title", input.Title.ToNString(), SqlDbType.VarChar, 50),
                    DB.CreateParameter("@ShortDescription", input.ShortDescription.ToNString(), SqlDbType.VarChar, 255),
                    DB.CreateParameter("@LongDescription", input.LongDescription.ToNString(), SqlDbType.Text),
                    DB.CreateParameter("@StartDate", input.StartDate.ToNString(), SqlDbType.SmallDateTime),
                    DB.CreateParameter("@EndDate", input.EndDate.ToNString(), SqlDbType.SmallDateTime),
                    DB.CreateParameter("@Link", input.Link.ToNString(), SqlDbType.VarChar, 255));
        }
    }
}
